<?php
namespace app\admin\traits;
use think\facade\Db;
use think\facade\Cache;

trait AdminAuth
{
    protected $permission_id = '';
    //这里的$actions = [空格不能去掉，生成代码的时候会匹配空格  下方空一行为了生成代码的时候填充代码 为了美观
    protected $actions = ['add' => 'add','del' => 'del', 'edit' => 'edit', 'hei' => 'hei', 'info' => 'info','daochu'=>'daochu',

		'addtest2'=>'addtest2','edittest2'=>'edittest2','deltest2'=>'deltest2',
		'addtttt'=>'addtttt','edittttt'=>'edittttt','deltttt'=>'deltttt',
		'addceshi'=>'addceshi','editceshi'=>'editceshi','delceshi'=>'delceshi',
		'addtest'=>'addtest','edittest'=>'edittest','deltest'=>'deltest',
		'addoldlist'=>'addoldlist','editoldlist'=>'editoldlist','deloldlist'=>'deloldlist',
		'addfuwen'=>'addfuwen','editfuwen'=>'editfuwen','delfuwen'=>'delfuwen',
		'addgouxuan'=>'addgouxuan','editgouxuan'=>'editgouxuan','delgouxuan'=>'delgouxuan',
		'addttt'=>'addttt','editttt'=>'editttt','delttt'=>'delttt',
		'addtestlist'=>'addtestlist','edittestlist'=>'edittestlist','deltestlist'=>'deltestlist',
		'adddaxiao'=>'adddaxiao','editdaxiao'=>'editdaxiao','deldaxiao'=>'deldaxiao',
		'addsqlxx'=>'addsqlxx','editsqlxx'=>'editsqlxx','delsqlxx'=>'delsqlxx',
        'adduser' => 'adduser','edituser' => 'edituser','deluser'=>'deluser','addimg' => 'addimg','editimg'=>'editimg','delimg'=>'delimg',
        'addannounce'=>'addannounce','editannounce'=>'editannounce','delannounce'=>'delannounce','editsywen' => 'editsywen',
        'addrole' => 'addrole','editrole' => 'editrole','delrole' => 'delrole','userinfo'=>'userinfo','addwen'=>'addwen',
        'editwen'=>'editwen','delwen'=>'delwen','addimglist'=>'addimglist','editimglist'=>'editimglist','delimglist'=>'delimglist',
        'addcalllist'=>'addcalllist','editcalllist'=>'editcalllist','delcalllist'=>'delcalllist','delmoneylog'=>'delmoneylog',
        'jiaohao'=>'jiaohao','guohao'=>'guohao','jiaohaogong'=>'jiaohaogong','delcurd'=>'delcurd','delintegrallog'=>'delintegrallog',
        'addtopnav'=>'addtopnav','edittopnav'=>'edittopnav','deltopnav'=>'deltopnav','codetopnav'=>'codetopnav','addcurd'=>'addcurd','editcurd'=>'editcurd',
        'delpayorder'=>'delpayorder','delincome'=>'delincome','delrefund'=>'delrefund','addrefund'=>'addrefund','editrefund'=>'editrefund',
        'addincome'=>'addincome','editincome'=>'editincome','addpayorder'=>'addpayorder','editpayorder'=>'editpayorder',
        'addmessage'=>'addmessage','editmessage'=>'editmessage','delmessage'=>'delmessage',
        'zjmoney'=>'zjmoney','setStatus'=>'setStatus',
    ];
    //这里的$action = [空格不能去掉，生成代码的时候会匹配空格  下方空一行为了生成代码的时候填充代码 为了美观
    private $action = ['index', 'userlist', 'rolelist','actionlog','cloudsite','remindsite','wxsite',

		'test2',
		'tttt',
		'ceshi',
		'test','oldlist',
		'fuwen',
		'gouxuan',
		'ttt',
		'testlist',
		'daxiao',
		'sqlxx','payorder','message',
        'calllist','integrallog','usermoneylog','income',
        'orderlist','speechsite','topnav','curdlist','refund',
        'userlist','userlist_hei','zhimg','announce','sydex','appsite','smssite','mapsite','imglist',
    ];
    private $immunity = ['getclassdata','sousuo','getpvtime','login'];
    public function __construct()
    {
        //parent::__construct();
        //判断是否登录
        if(!session('escort_admin_token')){
            $start=Cache::get('escort_loginno');
            if($start){
                $miao=time()-$start;
                if($miao>4){
                    Cache::tag("_escort_")->clear();  //清理
                    $url='/admin/Login/index?221';
                    header('Location:'.$url);exit;
                }
            }else{
                echo '<script>parent.location.reload();</script>';
                Cache::tag("_escort_")->set("escort_loginno",time());
            }
            echo '<script>parent.location.reload();</script>';exit;
        }
        //查询是否有按钮的权限
        if (in_array(request()->action(), $this->action) || in_array(request()->action(), $this->actions)) {
            
            //查询商家id是否存在
            $escort_admin_merid=session('escort_admin_merid');
            $where=[];
            if($escort_admin_merid){
                $where['merid']=$escort_admin_merid;
            }
            $username = session('escort_admin_name');
            $admin = Db::name('escort_admin')->where($where)->where('username', $username)->field('id, name')->find();
            
            //查看该用户的角色
            $role = Db::name('escort_admin_role')->alias('r')->join('escort_admin_role_permission p', 'r.id = p.role_id', 'LEFT')->join('escort_admin_user_role u', 'u.role_id = r.id', 'LEFT')->field('r.name, p.permission_id')->where('u.admin_id', $admin['id'])->find();
            $this->permission_id = $role['permission_id'];
            if(in_array(request()->action(), $this->action)){
                $permis = Db::name('escort_admin_permission')->where('controller', request()->controller())->where('action', request()->action())->value('id');
                if (!in_array($permis, explode(',', $role['permission_id']))) {
                    echo '你无权访问该控制器';exit;
                }
            }
            if(in_array(request()->action(), $this->actions)){
                $cont=request()->controller();
                $permis = Db::name('escort_admin_permission')->where('controller','like', '%'.$cont.'%')->where('action', request()->action())->value('id');
                if (!in_array($permis, explode(',', $role['permission_id']))) {
                    echo '你无权访问该方法';exit;
                }
            }
        }else if(in_array(request()->action(), $this->immunity)){

        }
        else{
            echo '<div style="font-size: 18px;font-weight: bold;">没有此方法</div>';exit;
        }
    }

    /**
     * 获取权限
     */
    public function getPermissions($controller)
    {
        $where = [];
        //查询商家id是否存在
        $escort_admin_merid=session('escort_admin_merid');
        $where=[];
        if($escort_admin_merid){
            //$where['merid']=$escort_admin_merid;
        }
    	$permis = Db::name('escort_admin_permission')->where($where)->where('controller', $controller)->where('id', 'in', $this->permission_id)->column('action');
    	return implode(',', $permis);
    }
    //获取商家id
    public function getMerid()
    {
        $escort_admin_merid=session('escort_admin_merid');
        return $escort_admin_merid;
    }
    /**
     * 纪录用户操作
     */
    public function writeActionLog($text)
    {
        $username = session('escort_admin_name');
        $escort_admin_merid=session('escort_admin_merid');
        $IP=request()->ip();
        $arr=[
            'user' => $username,
            'text' => $text,
            'addtime' => time(),
            'ip' => $IP
        ];
        if($escort_admin_merid){
            $arr['merid']=$escort_admin_merid;
        }
        Db::name('escort_admin_action_log')->insert($arr);
    }
}